<?php

namespace app\common\model\score;

use think\Model;
use think\Db;
use app\common\model\User;
use app\common\model\MoneyLog;
use app\common\model\score\UserTask;

class Task extends Model
{

    /**
     * 新手任务
     */
    const TASK_TYPE_NEWUSER = 0;

    /**
     * 日常任务
     */
    const TASK_TYPE_DAYLY = 1;

    // 表名
    protected $name = 'self_task';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text'
    ];

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 判断任务是否已经完成
     */
    public static function taskIsDone(Task $task_info, $user_id)
    {

        if ($task_info->limit_times < 1) {
            return FALSE;
        }

        if(isset($task_info['task_tag']) && $task_info['task_tag']){
            $where = [
                'task_tag' => $task_info['tag'],
                'user_id' => $user_id
            ];
        }else{
            $where = [
                'task_id' => $task_info['id'],
                'user_id' => $user_id
            ];
        }

        if ($task_info['type'] == self::TASK_TYPE_NEWUSER) {
            //新手任务
            $is_finished = UserTask::where($where)->count();
            if ($is_finished > 0) {
                return TRUE;
            }
        }elseif ($task_info['type'] == self::TASK_TYPE_DAYLY) {
            $today_start = strtotime(date('Y-m-d'));
            $today_end = strtotime(date('Y-m-d') . '+1 day');
            $is_finished = UserTask::where($where)
                ->where('createtime', ['>=', $today_start], ['<', $today_end], 'and')->count();
            if ($task_info['limit_times'] > 0 && $is_finished >= $task_info['limit_times']) {
                return TRUE;
            }
        } else {
            //
            $is_finished = UserTask::where($where)->count();
            if ($task_info['limit_times'] != 0 && $is_finished >= $task_info['limit_times']) {
                return TRUE;
            }
        }

        return FALSE;
    }

    /*
     * 执行任务方法
     */
    public static function execute_task($id, $user_id)
    {
        //任务是否可以执行
        $task_info = self::get($id);
        if (!$task_info || $task_info['status'] != 1) {
            return false;
        }
        
        if(self::taskIsDone($task_info, $user_id)){
            return false;
        }

        //执行任务 生成任务记录
        $user_task_data['task_id'] = $id;
        $user_task_data['task_tag'] = $task_info['tag'];
        $user_task_data['user_id'] = $user_id;
        $user_task_data['remark'] = $task_info['name'];
        $user_task_data['createtime'] = time();

        $res = UserTask::create($user_task_data);

        if (!$res) return false;

        if ($task_info['score'] > 0) {
            //积分操作
            User::score($task_info['score'], $user_id, $task_info['name']);
        }

        if ($task_info['gold'] > 0) {
            //金币操作
            User::money($task_info['gold'], $user_id, $task_info['name'], 0, MoneyLog::TYPE_SYSTEM);
        }
    }
}
